package com.rayc.mall.order.controller;

import com.rayc.mall.api.dto.OrderDTO;
import com.rayc.mall.api.dto.OrderFormDTO;
import com.rayc.mall.common.constant.Constants;
import com.rayc.mall.common.result.Result;
import com.rayc.mall.common.utils.BeanUtils;
import com.rayc.mall.core.annotation.CacheAddByUserId;
import com.rayc.mall.core.annotation.CacheRemoveByUserId;
import com.rayc.mall.order.domain.po.Order;
import com.rayc.mall.order.domain.vo.OrderDetailVO;
import com.rayc.mall.order.domain.vo.OrderVO;
import com.rayc.mall.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-26
 * @Description: 订单管理接口
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
@RequiredArgsConstructor
public class OrderController {
    private final IOrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping("/create")
    // 创建订单之后购物车会变化
    @CacheRemoveByUserId(keys = {Constants.REDIS_KEY_ORDERS, Constants.REDIS_KEY_CARTS})
    public Result<OrderVO> createOrder(@RequestBody OrderFormDTO orderFormDTO) {
        log.info("创建订单 {}", orderFormDTO);
        Order order = orderService.createOrder(orderFormDTO);
        OrderVO orderVO = BeanUtils.copyBean(order, OrderVO.class);
        log.info("创建订单成功 {}", orderVO);
        return Result.success(orderVO);
    }

    @ApiOperation("获取订单信息")
    @GetMapping("/list")
    @CacheAddByUserId(key = Constants.REDIS_KEY_ORDERS)
    public Result<List<OrderVO>> queryMyOrders() {
        log.info("查询订单信息列表");
        List<OrderVO> orders = orderService.queryMyOrders();
        log.info("查询订单信息列表成功 {}", orders);
        return Result.success(orders);
    }

    @ApiOperation("获取订单详细信息")
    @GetMapping("/detail/{id}")
    // 设置1小时的缓存时间（因为没有移除逻辑）
    @CacheAddByUserId(key = Constants.REDIS_KEY_ORDER_DETAIL, useArgs = true, expireSeconds = 3600)
    public Result<OrderDetailVO> queryOrderDetail(@PathVariable Long id) {
        log.info("查询订单详细信息");
        OrderDetailVO orderDetailVO = orderService.getOrderDetail(id);
        log.info("查询订单详细信息成功 {}", orderDetailVO);
        return Result.success(orderDetailVO);
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_ORDERS)
    public Result<String> cancelOrder(@ApiParam("订单id") @PathVariable("id") Long id) {
        log.info("取消订单 id: {}", id);
        orderService.cancelOrder(id);
        log.info("取消订单成功 id: {}", id);
        return Result.success();
    }

    @ApiOperation("确认订单完成")
    @PutMapping("/confirm/{id}")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_ORDERS)
    public Result<String> confirmOrder(@ApiParam("订单id") @PathVariable("id") Long id) {
        log.info("确认订单完成 id: {}", id);
        orderService.confirmOrder(id);
        log.info("确认订单完成成功 id: {}", id);
        return Result.success();
    }

    // feign interface
    @GetMapping("/listByUserId")
    public Result<List<OrderDTO>> queryByUserId(@RequestParam Long userId) {
        log.info("获取用户订单信息, userId: {}", userId);
        List<OrderDTO> orders = orderService.queryByUserId(userId);
        log.info("获取到用户{}的订单信息: {}", userId, orders);
        return Result.success(orders);
    }

    // feign interface
    @GetMapping
    public Result<OrderDTO> queryById(@RequestParam Long id) {
        log.info("根据id获取订单信息, id: {}", id);
        Order order = orderService.getById(id);
        log.info("根据id获取到订单信息 order: {}", order);
        return Result.success(BeanUtils.copyBean(order, OrderDTO.class));
    }

    // feign interface
    @PutMapping("/success/{id}")
    Result<String> markOrderPaySuccess(@PathVariable("id") Long orderId) {
        log.info("尝试标记订单支付成功, orderId: {}", orderId);
        orderService.markOrderPaySuccess(orderId);
        log.info("成功标记订单支付成功, orderId: {}", orderId);
        return Result.success();
    }
}
